var water = {
    initLoadWater: function () {
        var sTimeSecondes = new Date().getTime();
        var geojsonUrl = getRootPath() + "/attach/" + dataGuid + "/water.geojson?" + sTimeSecondes;
        $.ajax({
            url: geojsonUrl,
            type: "post",
            async: false,
            dataType: "JSON",
            success: function (result) {
                uploadWater = result;
                console.log(result);
                water.initWater(result);
            },
            error: function (result) {
                console.log("获取水面信息失败")
            }
        });
    },
    initWater: function (WaterData) {
        if (WaterData.length > 0) {
            $("#water").show();
            for (var a = 0; a < WaterData.length; a++) {
                var wateritem = new LSGlobe.LSWater(viewer);
                wateritem.waterPolygon = new LSGlobe.PolygonGeometry({
                    polygonHierarchy: new LSGlobe.PolygonHierarchy(LSGlobe.Cartesian3.fromDegreesArrayHeights(
						WaterData[a].waterArry
					)),
                    perPositionHeight: true
                });
                wateritem.initialize();
                wateritem.id = WaterData[a].id;
                wateritem.name = WaterData[a].name;
                wateritem.waveWidth = WaterData[a].waveWidth;
                wateritem.flowDirection = WaterData[a].flowDirection;
                wateritem.flowSpeed = WaterData[a].flowSpeed;
                wateritem._waterColor = LSGlobe.Color.fromCssColorString(WaterData[a]._waterColor); //LSGlobe.Color.DARKCYAN;
                viewer.scene.waterCollection.add(wateritem);
                water.waterHtml(WaterData[a]);
            }
        }
    },
    waterHtml: function (WaterData) {
        var oWaterHtml = "";
        oWaterHtml += "<div class=\"layer-item-title water\" id=\"" + WaterData.id + "\">";
        oWaterHtml += "   <i class=\"\"></i>";
        oWaterHtml += "   <i class=\"checkicon active\"></i>";
        oWaterHtml += "<i class=\"typeicon polygon\" style=\"background:" + WaterData._waterColor + "\"></i>";
        oWaterHtml += "<span>" + WaterData.name + "</span>";
        oWaterHtml += "</div>";
        $("#waterBox").append(oWaterHtml);
    },
    init: function () {
        $("#waterBox").on("click", ".layer-item-title", function () {
            var oThis = $(this);
            console.log("=================")
            var oId = oThis.attr("id");
            var Waters = viewer.scene.waterCollection._water;
            for (var a = 0; a < Waters.length; a++) {
                if (Waters[a].id == oId) {
                    var Pos = Waters[a]._waterCenterPos;
                    var cartographic = LSGlobe.Cartographic.fromCartesian(Pos);
                    var currentClickLon = LSGlobe.Math.toDegrees(cartographic.longitude);
                    var currentClickLat = LSGlobe.Math.toDegrees(cartographic.latitude);
                    var oHeight = Waters[a]._zFactor;
                    viewer.camera.flyTo({
                        destination: LSGlobe.Cartesian3.fromDegrees(currentClickLon, currentClickLat, oHeight + 50),
                        duration: 2
                    })
                }
            }
        });
        //水面显示隐藏
        $(document).on("click", "#analysis-panel .layer-item.water .checkicon", function () {
            var oThis = $(this);
            if (oThis.hasClass("active")) {
                oThis.removeClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").removeClass("active");
                    var Waters = viewer.scene.waterCollection._water;
                    for (var a = 0; a < Waters.length; a++) {
                        Waters[a].isVisible = false;
                    }
                } else {
                    var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if (oParentBox.length <= 0) {
                        oThis.parents(".layer-item-content").prev().find(".checkicon").removeClass("active");
                    }
                    var oGuid = oThis.parent().attr("id");
                    var Waters = viewer.scene.waterCollection._water;
                    for (var a = 0; a < Waters.length; a++) {
                        if (Waters[a].id == oGuid) {
                            Waters[a].isVisible = false;
                        }
                    }
                }
            } else {
                oThis.addClass("active");
                if (oThis.hasClass("box")) {
                    var oChilds = oThis.parent().next().find(".checkicon");
                    var oChildChange = oThis.parent().next().find(".checkicon").addClass("active");
                    var Waters = viewer.scene.waterCollection._water;
                    for (var a = 0; a < Waters.length; a++) {
                        Waters[a].isVisible = true;
                    }
                } else {
                    var oParentChild = oThis.parents(".layer-item-content").children().length;
                    var oParentBox = oThis.parents(".layer-item-content").find(".checkicon.active");
                    if (oParentChild == oParentBox.length) {
                        oThis.parents(".layer-item-content").prev().find(".checkicon").addClass("active");
                    }
                    var oGuid = oThis.parent().attr("id");
                    var Waters = viewer.scene.waterCollection._water;
                    for (var a = 0; a < Waters.length; a++) {
                        if (Waters[a].id == oGuid) {
                            Waters[a].isVisible = true;
                        }
                    }
                }
            }

            return false;
        });
        $("#waterBox").on("click", ".monomerItem", function () {
            var oThis = $(this);
            var oId = oThis.attr("id");
            var Waters = viewer.scene.waterCollection._water;
            for (var a = 0; a < Waters.length; a++) {
                if (Waters[a].id == oId) {
                    var Pos = Waters[a]._waterCenterPos;
                    var cartographic = LSGlobe.Cartographic.fromCartesian(Pos);
                    var currentClickLon = LSGlobe.Math.toDegrees(cartographic.longitude);
                    var currentClickLat = LSGlobe.Math.toDegrees(cartographic.latitude);
                    var oHeight = Waters[a]._zFactor;
                    viewer.camera.flyTo({
                        destination: LSGlobe.Cartesian3.fromDegrees(currentClickLon, currentClickLat, oHeight + 50),
                        duration: 2
                    })
                }
            }
        });
    }
}